package com.maker.mcp.client.controller;


import com.maker.mcp.client.bean.ChatEntity;
import com.maker.mcp.client.service.ChatService;
import com.maker.mcp.client.service.RagService;
import com.maker.mcp.client.utils.MakerResult;
import jakarta.annotation.Resource;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.document.Document;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import reactor.core.publisher.Flux;

import java.util.List;

@RestController
@RequestMapping("rag")
public class RagController {
    @Resource
    private RagService ragService;
    @Resource
    private ChatService chatService;

    @PostMapping("uploadRagDoc")
    public MakerResult uploadRagDoc(@RequestParam("file") MultipartFile file){

        List<Document>  list =ragService.loadText(file.getResource(),file.getOriginalFilename());

        return MakerResult.ok(list);
    }
    //测试使用
    @GetMapping("/doSearch")
    public MakerResult doSearch(@RequestParam String question){

        List<Document> documents = ragService.doSearch(question);

        return MakerResult.ok(documents);
    }



    @PostMapping("/search")
    public void search(@RequestBody ChatEntity chatEntity){

        List<Document> documents = ragService.doSearch(chatEntity.getMessage());
        chatService.doChatRagSearch(chatEntity,documents);
    }

    @RequestMapping(value = "/chat", produces="text/html;charset=utf-8")
    public Flux<String> chat(String memoryId,String message){
        String cuurentUserName ="神奇侠测试";
        ChatEntity  chatEntity = new ChatEntity(cuurentUserName,message,memoryId);
        List<Document> documents = ragService.doSearch(message);
        Flux<String> stringFlux = chatService.doChatRagSearchV2(chatEntity, documents);

        return stringFlux;
    }


}
